<?php
/*
Files :          commandFile.php, (require ConfFile.php)
Name :           Derived class for manipulation with command.conf ("Conf-File ver. (0.11)")
Version :        1.2
Release :        30. 1. 2007
Descript :       Derived class for manipulation with command.conf ("Conf-File ver. (0.11)")
                 derived form ConfFile

Functions:
     Derived form ConfFile class :
        public function GetData()
        public function Open($filename)
        public function Close()
        public function Load()
        public function AddItem($block,$parametr,$value)
        public function EditItem($block, $item, $index, $new_value)
        public function DeleteItem($block, $item, $index)
        public function AddBlock($block)
        public function DeleteBlock($block)
        public function RenameBlock($block, $newblock)

     New functions in class:
        public function FindFile($text)      -> returns information about usre or false

*/

require_once("ConfFile.php");

class userFile extends ConfFile
{
  public function __construct($filename="n/a")
  {
    parent::__construct($filename);
  }
  
  /*
   *  Generate mask for bot user from user hostmask
   */
  public function ShortMask($mask)
  {
   if(strlen($mask)>0)
    {
    eregi("^[~]?([^@]*)@(.*)\$",$mask,$part);
    // $part[1] @ $part[2] 
    if (eregi("(.*)\.(.*)$",$part[1],$b))
      {$mask="*!*".$b[2];}
    else
      {
        if(substr($mask,0,1)=="~")
         {$mask="*!*".$part[1];}
        else
         {$mask="*!".$part[1];}
      }
    $mask.="@";

    if (eregi("((([0-2]?[0-9]?[0-9]\.){3})|(([0-2]?[0-9]?[0-9]\.){5}))([0-2]?[0-9]?[0-9]){1}",$part[2], $ip_split))
       $mask.=$ip_split[1]."*";
    else      // its hostmask
     {
      if(eregi("(.*)\.([^\.]*)\.([^\.]*)\.([^\.]*)",$part[2],$c))
          $mask.="*.".$c[2].".".$c[3].".".$c[4];
      else
          $mask.=$part[2];
     }
     return $mask;
    }
   else
    {
      return false;
    }
  }
  /*
   *  Returns Username, if hostmask in $who match with mask of some user or FALSE
   */	
  public function MaskToUser($who)
  {
	$result=false;
	
	$src = array('*');
	$trg = array('.*');
  	
	foreach($this->Data as $UserName=>$User )
	{
  	   foreach ($User['mask'] as $Mask)
  	   {
  	   		if (ereg(str_replace($src,$trg,$Mask),$who)) 
    		{$result=$UserName; break;} 
  		}
  	   if ($result!=false) break;	
	}
	return $result;
  }
  /*
   *  Returns user info or FALSE
   */
  public function GetUserInfo($user)
  {
  	return isset($this->Data[$user])?$this->Data[$user]:FALSE; 
  }
  /*
   *  Returns TRUE if $user has $flags for $channel, or has global $flag
   */
  public function HasFlag($user,$flags,$channel='only_global')
  {
  	$channel=strtolower($channel);
	if (isset($this->Data[$user]))
	{
		if (!is_array($flags)) $flag=Array($flags);
  		$result=false;
		foreach($flags as $flag)
  		{
  	 	 if ($channel=='only_global') $result=in_array($flag,$this->Data[$user]['flag']);
  		 else $result=in_array($flag,$this->Data[$user]['flag'])?true:in_array($flag,$this->Data[$user]['flag>'.$channel]);	
  		 if($result) break;
  		}
  		return $result;
	}
  	else return false;
  }
  /*
   * Returns Flags for mask
   */
  public function Flags($mask, $kde="global") //vrati flagy uzivatele ktereho pozna podle masky, $kde je nepovinny a znaci ktere flagy chces videt (globalni | na #sg.cz nebo tak) - pokud uzivatel neexistuje vraci FALSE
  {
       $nick=$this->MaskToUser($mask);
           if (!$nick)
             {return false;}
           else
             {
                   $block_name="flag";
                   if ($kde!="global") $block_name.=">".$kde;
                   if (isset($this->Data[$nick][$block_name])) return $this->Data[$nick][$block_name];
                   else return array();
                 }
  }
  /*
   * Returns true, if $nick exists (non case-sensitive)
   */
  public function UserExists($nick)
  {
	 $exists = false;
	 foreach($this->Data as $UserName=>$User)
	 {
		if (strtolower($UserName) == strtolower($nick)) {$exists = true; break;}
	 }
	 return $exists;
  }
  /*
   * Adds user
   */
  public function AddUser($nick, $mask)
  {
	if(!$this->UserExists($nick))
	{
		$this->Data[$nick]['mask'] = $mask;
		$this->Save();
	}
	else return false; // Username exists
  }	
}
?>
